package com.hanlin.day03;

import com.hanlin.day01.DataSort;

/**
 * 对数器  生成
 *
 * @Author: hl.yuan
 * @Date: 2021/5/28 11:12
 */
public class Alternator {
    /**
     * 生成随机数组
     *
     * @param maxValue 范围在[0,maxValue]
     * @param maxIndex 数组长度[0,,maxIndex-1]
     * @return
     */
    public static int[] generateArray(int maxValue, int maxIndex) {
        int[] array = new int[maxIndex];
        for (int i = 0; i < maxIndex; i++) {
            array[i] = (int) (Math.random() * maxValue) + 1;
        }
        return array;
    }

    public static void printArray(int[] array) {
        for (int i : array) {
            System.out.print(i + " ");
        }
        System.out.println();
    }

    /**
     * 判断array是不是有序的
     *
     * @param array
     * @return
     */
    public static boolean checkArray(int[] array) {
        if (array.length < 2) {
            return true;
        }
        // 定义一个判断条件，当不是有序的，就变成了false了
        boolean check = true;
        for (int i = 0; i < array.length; i++) {
            if (i != array.length - 1 && array[i] > array[i + 1]) {
                check = false;
            }
        }
        return check;
    }

    public static int[] copyArray(int[] array){
        int[] copy = new int[array.length];
        for (int i = 0; i < array.length; i++) {
            copy[i] = array[i];
        }
        return copy;

    }

    public static void main(String[] args) {
        int index = 1000000;
        int maxValue = (int) (Math.random() * 10000 + 1);
        int maxIndex = (int) (Math.random() * 200 + 1);
        for (int i = 0; i < index; i++) {
            int[] array = generateArray(maxValue, maxIndex);
            int[] copyArray = copyArray(array);
            DataSort.selectSort(array);
            boolean checkArray = checkArray(array);
            if (!checkArray){
                System.out.println("排序错误");
                printArray(copyArray);
            }
        }


    }
}
